import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';

export default (({ command, mode }) => {

  let env = {}
  const isBuild = command === 'build'
  if (!isBuild) {
    env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), process.cwd())
  } else {
    env = loadEnv(mode, process.cwd())
  }
  // console.log('----', env, mode, command, process.argv[3])

  return defineConfig({

    plugins: [react(), tsconfigPaths()],
    server: {
      proxy: {
        //api是自行设置的请求前缀，任何请求路径以/api开头的请求将被代理到对应的target目标
        '/app-api': {
          target: 'http://localhost:8980', //目标域名
          changeOrigin: true, //需要代理跨域
          rewrite: (path) => path.replace(/^\/api/, ''), //路径重写，把'/api'替换为''
        },
        '/uploads': {
          target: 'http://localhost:8980', //目标域名
          changeOrigin: true, //需要代理跨域
        }
      },
    },
    resolve: {
      alias: {
        '@': '/src',
      },
    },
    test: {
      globals: true,
      environment: 'jsdom',
      setupFiles: './vitest.setup.mjs',
    },
  });
});
